module page {

    export class RankPage extends egret.Sprite {

        public static HIDE: number = 0;
        public static SHOW: number = 1;
        public static SUBMIT: number = 2;
        public static readonly SUBMIT_SCORE_NOT_SHOW_RANK: number = 3;
        public static readonly SHOW_GAME_OVER_RANK: number = 4;
        public static readonly SHOW_GROUP_RANK: number = 5;

        public static readonly PREV_PAGE: number = 10006;

        public static readonly NEXT_PAGE: number = 10007;

        public static readonly SHOW_LIMIT_TIME_RANK: number = 10008;

        public static readonly GET_MY_AVATAR: number = 10013;

        private shareBitmap: egret.Bitmap;

        private rankBitmapData: egret.BitmapData;

        public static readonly PK_CANVAS_WIDTH: number = 600;

        public static readonly PK_CANVAS_HEIGHT: number = 769;

        public constructor() {
            super();
        }

        public hide(): void {
            if (this.contains(this.shareBitmap)) {
                this.removeChild(this.shareBitmap);
            }
            egret.stopTick(this.tickHandler, this);
        }

        public showFullscreen(command: number, scoreData: Array<any>): void {

            console.log('showFullscreen')

            if (!manager.GameManager.isProduct) {
                return;
            }

            let canvas = manager.SystemManager.getSharedCanvas();
            let scale = manager.SystemManager.canvasWidth / 750;
            canvas.width = manager.DisplayManager.STAGE_WIDTH * scale;
            canvas.height = manager.DisplayManager.STAGE_HEIGHT * scale;

            this.rankBitmapData = new egret.BitmapData(canvas);
            this.rankBitmapData.$deleteSource = false;
            const texture = new egret.Texture();
            texture._setBitmapData(this.rankBitmapData);
            this.shareBitmap = new egret.Bitmap(texture);
            this.shareBitmap.width = manager.DisplayManager.STAGE_WIDTH;
            this.shareBitmap.height = manager.DisplayManager.STAGE_HEIGHT;
            this.addChild(this.shareBitmap);

            egret.startTick(this.tickHandler, this);

            let openDataContext = wx.getOpenDataContext();
            openDataContext.postMessage({
                command: command,
                keyList: ["score"],
                scoreData: scoreData,
                openid: manager.GameManager.getInstance().openid,
            });
        }

        public static sendCommand(command: number, extra: any = null): void {
            let openDataContext = wx.getOpenDataContext();
            openDataContext.postMessage({
                command: command,
                extra: extra,
            });
        }

        public show(command: number, scoreData: Array<any>, keyList: Array<string> = ['score']): void {
            if (!manager.GameManager.isProduct) {
                return;
            }

            let canvas = manager.SystemManager.getSharedCanvas();
            let scale = manager.SystemManager.canvasWidth / 750;
            canvas.width = RankPage.PK_CANVAS_WIDTH * scale;
            canvas.height = RankPage.PK_CANVAS_HEIGHT * scale;

            if (this.rankBitmapData != null) {
                this.rankBitmapData.$dispose();
                this.rankBitmapData = null;
            }
            this.rankBitmapData = new egret.BitmapData(canvas);
            this.rankBitmapData.$deleteSource = false;
            const texture = new egret.Texture();
            texture._setBitmapData(this.rankBitmapData);
            this.shareBitmap = new egret.Bitmap(texture);
            this.shareBitmap.width = RankPage.PK_CANVAS_WIDTH;
            this.shareBitmap.height = RankPage.PK_CANVAS_HEIGHT;
            this.addChild(this.shareBitmap);
            egret.startTick(this.tickHandler, this);

            let openDataContext = wx.getOpenDataContext();
            openDataContext.postMessage({
                command: command,
                keyList: keyList,
                scoreData: scoreData,
                openid: manager.GameManager.getInstance().openid,
            });

        }

        // public show(command: number, scoreData: Array<any>): void {
        //     console.log('rankPage.SHOW')
        //     this.rankBitmapData = new egret.BitmapData(window["sharedCanvas"]);
        //     this.rankBitmapData.$deleteSource = false;
        //     const texture = new egret.Texture();
        //     texture._setBitmapData(this.rankBitmapData);
        //     this.shareBitmap = new egret.Bitmap(texture);
        //     this.shareBitmap.width = manager.DisplayManager.STAGE_WIDTH;
        //     this.shareBitmap.height = manager.DisplayManager.STAGE_HEIGHT;
        //     this.shareBitmap.x = 0
        //     this.shareBitmap.y = 0
        //     this.addChild(this.shareBitmap);

        //     egret.startTick(this.tickHandler, this);

        //     console.log('与开放数据域通信' + command)
        //     //与开放数据域通信
        //     let openDataContext = wx.getOpenDataContext();
        //     openDataContext.postMessage({
        //         command: command,
        //         keyList: ["score"],
        //         stageWidth: manager.DisplayManager.STAGE_WIDTH,
        //         stageHeight: manager.DisplayManager.STAGE_HEIGHT,
        //         scoreData: scoreData
        //     });
        // }

        public commit(command: number, scoreData: Array<any>): void {
            let openDataContext = wx.getOpenDataContext();
            openDataContext.postMessage({
                command: command,
                keyList: ["score"],
                scoreData: scoreData
            });
        }
 
        public stop():void {
            egret.stopTick(this.tickHandler, this);
            this.removeChild(this.shareBitmap)
        }
        public static DT: number = 50;

        private lastAt: number = 0;

        private tickHandler(dt: number): boolean {
            if (this.lastAt < 1) {
                this.lastAt = dt;
                return false;
            }
            if (dt - this.lastAt < RankPage.DT) {
                return true;
            }
            this.lastAt = dt;
            egret.WebGLUtils.deleteWebGLTexture(this.rankBitmapData.webGLTexture);
            this.rankBitmapData.webGLTexture = null;
            return true;
        }

    }
}